#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
       btn = WaitforElementById(driver,'com.eoemobile.netmarket:id/iv_actionbar_download',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
       btn = WaitforElementById(driver,'com.eoemobile.netmarket:id/download_menu_more',30)
    except NoSuchElementException:
        driver.back()
        return
    else:
        btn.click()

    try:
        btn = WaitforElementByName(driver,u'全部取消',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    driver.back()

def clickappdown(driver):
    try:
        down_btns=WaitforElementsById(driver,'com.eoemobile.netmarket:id/ll_default',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        for item in down_btns:
            try:
                size=WaitforElementById(item,"com.eoemobile.netmarket:id/tv_default_size",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[0:-3])<10:
                    continue
            try:
                down_btn=WaitforElementById(item,'com.eoemobile.netmarket:id/iv_download',30)
            except NoSuchElementException:
                continue
            down_btn.click()
            waittime=30
            while waittime:
                text=down_btn.get_attribute("text")
                print text
                if '0%' not in text:
                    down_btn.click()
                    break
                else:
                    time.sleep(0.5)
                    if waittime==0:
                        down_btn.click()
                        break
                    waittime=waittime-1
            print "----------------------------------------------"
    #driver.back()



def soft(driver):
    categorys = [u'系统个性化',u'社交',u'购物',u'娱乐',u'通讯',u'工具',u'效率与办公',u'阅读与图书',u'杂志资讯',u'旅游与出行',u'天气',u'生活时尚',u'财经',u'摄影与录像',u'健康',u'教育',u'音乐']
    try:
        #WaitforElementByName(driver,u'软件',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def game(driver):
    categorys = [u'动作冒险',u'经营策略',u'棋牌天地',u'益智休闲',u'飞行射击',u'角色扮演',u'体育竞技',u'网络游戏']
    try:
        #WaitforElementByName(driver,u'游戏',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION          # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.eoemobile.netmarket'    # apk的包名
    desired_caps['appActivity'] = '.SplashActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    #driver.find_element_by_name().tag_name
    del_appdown_task(driver)
    soft(driver)
    time.sleep(2)
    game(driver)
    driver.quit()

#run()